package com.situ.object.service.impl;


import com.situ.object.dao.UserDao;
import com.situ.object.dao.impl.UserDaoImpl;
import com.situ.object.entity.User;
import com.situ.object.service.UserService;

import java.util.List;

//import com.situ.object.entity.Dept;

public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    //登录
    @Override
    public User login(User user) throws Exception {

        String username = user.getUsername();
        String password = user.getPassword();
        if (username.length() < 3 || username.length() > 16){
            throw new Exception("用户名长度应该为3—16位");
        }
        if (password.length() < 3 || password.length() > 16){
            throw new Exception("密码长度应该为3—16位");
        }
        //用户是否存在，根据用户名查询用户信息
        User sUser = userDao.selectByUserName(username);
        if (sUser == null){
            throw new Exception("用户不存在");
        }
        //密码是否正确
        if (!password.equals(sUser.getPassword())){
            throw new Exception("密码输入错误");
        }
        //状态是否有效
        if (sUser.getStatus() == 1){
            throw new Exception("状态无效");
        }

        return sUser;
    }

    //查询所有用户信息
    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    //修改密码
    @Override
    public int modifiedPwdUser(User user, String password, String newPassword, String rePassword) throws Exception {
//        后端验证密码格式
        //验证新密码格式
        if (newPassword.length() < 3 || newPassword.length() > 16){
            throw new Exception("密码长度应该为3—16位");
        }
        //验证旧密码是否错误

        User oldUser = userDao.selectById(user.getId());
        if (!oldUser.getPassword().equals(password)){
            throw new Exception("旧密码错误");
        }
        //新旧密码不一样
        if (password.equals(newPassword)){
            throw new Exception("新旧密码一样");
        }
        //确认密码正确
        if (!newPassword.equals(rePassword)){
            throw new Exception("确认密码不正确");
        }
        return userDao.updatePassword(user.getId(),newPassword);
    }

    //添加管理员
    @Override
    public int addUser(User user) {
        //判断是否重名
        return 0;
    }


}

